package com.quick.common.core.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.core.page.PageDomain;
import com.quick.common.core.page.TableSupport;

import java.util.List;

/**
 * web层通用数据处理
 *
 * @Author: Administrator
 * @Date: 2023/7/17
 */
public class BaseController {


    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, pageDomain.getOrderBy()).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    protected void clearPage() {
        PageHelper.clearPage();
    }

    /**
     * 响应请求分页数据
     */
    protected ApiResponse paging(List<?> list) {
        return success(list).put(ApiResponse.TOTAL, new PageInfo<>(list).getTotal());
    }

    /**
     * 响应请求分页数据
     */
    protected ApiResponse total(List<?> list) {
        return success(list).put(ApiResponse.TOTAL, list.size());
    }

    /**
     * 响应成功数据
     */
    protected ApiResponse success() {
        return ApiResponse.success();
    }

    /**
     * 响应成功数据
     */
    protected ApiResponse success(Object data) {
        return ApiResponse.success(data);
    }

    /**
     * 响应成功数据
     */
    protected ApiResponse success(int row) {
        if (row > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 响应失败数据
     */
    protected ApiResponse error() {
        return ApiResponse.error();
    }

    /**
     * 响应失败数据
     */
    protected ApiResponse error(String msg) {
        return ApiResponse.error(msg);
    }
}
